layui.define(["table", "form", "admin"], function (exports) {
  var $ = layui.$,
    table = layui.table,
    form = layui.form,
    admin = layui.admin;

  //
  table.render({
    elem: "#LAY-football-matchevent-manage",
    url: "/admin/basketball/matcheventlist",
    cols: [
      [
        { field: "id", width: 100, title: "ID", sort: true },
        {
          field: "",
          title: "Logo",
          width: 100,
          templet: function (d) {
            return (
              '<img style="display: inline-block; width: auto; height: 100%;" layadmin-event="photosPreview" src="' +
              d.logo +
              '"  value="' +
              d.logo +
              '">'
            );
          },
        },
        { field: "name_zh", title: "名称", width: 200 },
        { field: "name_zht", title: "繁体名称", width: 200 },
        { field: "name_en", title: "英文名称", width: 200 },
        { field: "short_name_zh", title: "名称缩写", width: 200 },
        { field: "short_name_zht", title: "繁体名称缩写", width: 200 },
        { field: "short_name_en", title: "英文名称缩写", width: 200 },
        {
          field: "",
          title: "赛事类别",
          width: 150,
          templet: function (d) {
            switch (d.type) {
              case 1:
                return "联赛";
              case 2:
                return "杯赛";
              case 3:
                return "友谊赛";
              default:
                return "未知";
            }
          },
        },
        {
          field: "",
          title: "所属区域",
          width: 150,
          templet: function (d) {
            if (d.area) {
              return d.area.name_zh;
            }
            return "";
          },
        },
        {
          field: "",
          title: "所属国家",
          width: 150,
          templet: function (d) {
            if (d.country) {
              return d.country.name_zh;
            }
            return "";
          },
        },
      ],
    ],
    page: true,
    limit: 50,
    height: "full-140",
    text: "对不起，加载出现异常！",
    even: true,
  });

  table.render({
    elem: "#LAY-football-team-manage",
    url: "/admin/basketball/teamlist",
    cols: [
      [
        { field: "id", width: 100, title: "ID", sort: true },
        {
          field: "",
          title: "Logo",
          width: 100,
          templet: function (d) {
            return (
              '<img style="display: inline-block; width: auto; height: 100%;" layadmin-event="photosPreview" src="' +
              d.logo +
              '"  value="' +
              d.logo +
              '">'
            );
          },
        },
        { field: "name_zh", title: "名称", width: 200 },
        { field: "name_zht", title: "繁体名称", width: 200 },
        { field: "name_en", title: "英文名称", width: 200 },
        { field: "short_name_zh", title: "名称缩写", width: 200 },
        { field: "short_name_zht", title: "繁体名称缩写", width: 200 },
        { field: "short_name_en", title: "英文名称缩写", width: 200 },
        { field: "found", title: "建队年份", width: 150 },
        { field: "website", title: "官网", width: 200 },
        {
          field: "",
          title: "所属赛事",
          width: 150,
          templet: function (d) {
            if (d.matchevent) {
              return d.matchevent.name_zh;
            }
            return "";
          },
        },
      ],
    ],
    page: true,
    limit: 50,
    height: "full-140",
    text: "对不起，加载出现异常！",
    even: true,
  });

  table.render({
    elem: "#LAY-football-match-manage",
    url: "/admin/basketball/matchlist",
    cols: [
      [
        { field: "id", width: 100, title: "比赛ID", sort: true },
        {
          field: "",
          title: "所属赛事",
          width: 200,
          templet: function (d) {
            if (d.matchevent) {
              return d.matchevent.name_zh + "(" + d.matchevent.id + ")";
            } else {
              return "- -";
            }
          },
        },
        {
          field: "",
          title: "主队",
          width: 200,
          templet: function (d) {
            if (d.hometeam) {
              return d.hometeam.name_zh + "(" + d.hometeam.id + ")";
            } else {
              console.log("主队查無資料", d);
              return "??????" + "(" + d.home_id + ")";
            }
          },
        },
        {
          field: "",
          title: "客队",
          width: 200,
          templet: function (d) {
            if (d.awayteam) {
              return d.awayteam.name_zh + "(" + d.awayteam.id + ")";
            } else {
              console.log("客队查無資料", d);
              return "??????" + "(" + d.away_id + ")";
            }
          },
        },
        {
          field: "",
          title: "比赛状态",
          width: 100,
          templet: function (d) {
            switch (d.status) {
              case 0:
                return "比赛异常";
              case 1:
                return "未开赛";
              case 2:
                return "上半场";
              case 3:
                return "中场";
              case 4:
                return "下半场";
              case 5:
                return "加时赛";
              case 7:
                return "点球决战";
              case 8:
                return "完场";
              case 9:
                return "推迟";
              case 10:
                return "中断";
              case 11:
                return "腰斩";
              case 12:
                return "取消";
              case 13:
                return "待定";
            }
          },
        },
        {
          field: "",
          title: "比赛时间",
          width: 200,
          templet: function (d) {
            return formatTimeStamp(d.time);
          },
        },
        {
          field: "",
          title: "常规时间比分",
          width: 120,
          templet: function (d) {
            return d.home_score + " : " + d.away_score;
          },
        },
        {
          field: "",
          title: "加时赛比分",
          width: 120,
          templet: function (d) {
            return d.home_otscrore + " : " + d.away_otscrore;
          },
        },
        {
          field: "",
          title: "点球战比分",
          width: 120,
          templet: function (d) {
            return d.home_pointscore + " : " + d.away_pointscore;
          },
        },
        {
          field: "",
          title: "开球时间",
          width: 200,
          templet: function (d) {
            return formatTimeStamp(d.kickoff_time);
          },
        },
        {
          field: "",
          title: "讯源地址",
          width: 500,
          templet: function (d) {
            if (d.url) {
              return d.url.pushurl1;
            } else {
              return "- -";
            }
          },
        },
        {
          title: "操作",
          width: 300,
          align: "center",
          fixed: "right",
          toolbar: "#table-match-list",
        },
      ],
    ],
    page: true,
    limit: 50,
    height: "full-140",
    text: "对不起，加载出现异常！",
    even: true,
  });

  table.render({
    elem: "#LAY-football-recmatch-manage",
    url: "/admin/basketball/recmatchlist",
    cols: [
      [
        { field: "id", width: 100, title: "序号", sort: true },
        {
          field: "",
          title: "比赛ID",
          width: 100,
          templet: function (d) {
            return d.match_id;
          },
        },
        {
          field: "",
          title: "所属赛事",
          width: 250,
          templet: function (d) {
            if (d.matchevent_id) {
              return d.matchevent_name_zh + "(" + d.matchevent_id + ")";
            } else {
              return "";
            }
          },
        },
        {
          field: "",
          title: "主队",
          width: 250,
          templet: function (d) {
            return d.hometeam_name_zh + "(" + d.home_id + ")";
          },
        },
        {
          field: "",
          title: "客队",
          width: 250,
          templet: function (d) {
            return d.awayteam_name_zh + "(" + d.away_id + ")";
          },
        },
        {
          field: "",
          title: "比赛状态",
          width: 100,
          templet: function (d) {
            switch (d.status) {
              case 0:
                return "比赛异常";
              case 1:
                return "未开赛";
              case 2:
                return "上半场";
              case 3:
                return "中场";
              case 4:
                return "下半场";
              case 5:
                return "加时赛";
              case 7:
                return "点球决战";
              case 8:
                return "完场";
              case 9:
                return "推迟";
              case 10:
                return "中断";
              case 11:
                return "腰斩";
              case 12:
                return "取消";
              case 13:
                return "待定";
            }
          },
        },
        {
          field: "",
          title: "比赛时间",
          width: 250,
          templet: function (d) {
            return formatTimeStamp(d.time);
          },
        },
        {
          field: "",
          title: "常规时间比分",
          width: 150,
          templet: function (d) {
            return d.home_score + " : " + d.away_score;
          },
        },
        {
          field: "",
          title: "加时赛比分",
          width: 150,
          templet: function (d) {
            return d.home_overtime + " : " + d.away_overtime;
          },
        },
        {
          field: "",
          title: "开球时间",
          width: 250,
          templet: function (d) {
            return formatTimeStamp(d.kickoff_time);
          },
        },
        {
          title: "操作",
          width: 200,
          align: "center",
          fixed: "right",
          toolbar: "#table-recmatch-list",
        },
      ],
    ],
    page: true,
    limit: 50,
    height: "full-140",
    text: "对不起，加载出现异常！",
    even: true,
  });

  table.render({
    elem: "#LAY-football-matchanchor-manage",
    url: "/admin/basketball/getmatchanchorlist",
    where: { matchid: $("#matchid").val() },
    cols: [
      [
        { field: "id", width: 100, title: "ID", sort: true, fixed: "left" },
        { field: "account", title: "账号", width: 150 },
        {
          field: "nick_name",
          title: "昵称",
          minWidth: 150,
          templet: function (d) {
            return decodeURI(d.nick_name);
          },
        },
        {
          field: "",
          title: "头像",
          width: 80,
          templet: function (d) {
            if (d.avatar) {
              return (
                '<img style="display: inline-block; width: 100%; height: 100%;" src= "' +
                d.avatar +
                '">'
              );
            } else {
              return "无";
            }
          },
        },
        { field: "anchor_level", title: "主播星级", width: 100 },
        {
          field: "gender",
          width: 80,
          title: "性别",
          templet: function (d) {
            if (!d.profile) {
              return '未知';
            }
            switch (d.profile.gender) {
              case 1:
                return '男';
              case 2:
                return '女';
              default:
                return "未知";
            }
          },
        },
        { field: "diamond", width: 80, title: "宝石" },
        {
          field: "online_status",
          width: 100,
          title: "在线状态",
          templet: function (d) {
            switch (d.online_status) {
              case 1:
                return '<span class="layui-badge layui-bg-green">在线</span>';
              case 2:
                return '<span class="layui-badge layui-bg-orange">暂离</span>';
              case 3:
                return '<span class="layui-badge layui-bg-red">通话中</span>';
              case 9:
                return '<span class="layui-badge layui-bg-black">离线</span>';
              default:
                return "";
            }
          },
        },
        {
          field: "",
          width: 100,
          title: "分成比例",
          templet: function (d) {
            return d.sharing_ratio + "%";
          },
        },
        {
          title: "操作",
          width: 150,
          align: "center",
          fixed: "right",
          toolbar: "#table-matchanchor-list",
        },
      ],
    ],
    page: true,
    limit: 20,
    height: "full-200",
    text: "对不起，加载出现异常！",
    even: true,
  });

  //查看照片
  admin.events.photosPreview = function (othis) {
    let urls = othis.attr("value");
    let urlarr = new Array();
    $.each(urls.split(","), function (i, val) {
      urlarr.push({ src: val });
    });
    layer.photos({
      photos: {
        title: "查看照片", //相册标题
        data: urlarr,
      },
      shade: 0.01,
      closeBtn: 1,
      anim: 5,
    });
  };

  //监听工具条
  table.on("tool(LAY-football-match-manage)", function (obj) {
    if (obj.event === "setrecmatch") {
      layer.confirm("确认推荐该比赛？", function (index) {
        admin.req({
          url: "/admin/basketball/recmatch_post",
          data: {
            id: obj.data.id,
            data_source_id: obj.data.data_source_id,
            third_id: obj.data.third_id,
          },
          success(res) {
            if (res.code == 0) {
              table.reload("LAY-football-match-manage");
              layer.msg(res.msg, { icon: 1 });
            } else {
              layer.msg(res.msg, { icon: 5 });
            }
          },
        });
      });
    } else if (obj.event === "unsetrecmatch") {
      layer.confirm("确认取消推荐该比赛？", function (index) {
        admin.req({
          url: "/admin/football/unsetrecmatch_post",
          data: { id: obj.data.id },
          success(res) {
            if (res.code == 0) {
              table.reload("LAY-football-match-manage");
              layer.msg(res.msg, { icon: 1 });
            } else {
              layer.msg(res.msg, { icon: 5 });
            }
          },
        });
      });
    } else if (obj.event === "anchor") {
      var index = layer.open({
        type: 2,
        title: "关联主播",
        content: "/admin/basketball/matchanchor?id=" + obj.data.id,
        maxmin: true,
      });
      layer.full(index);
    } else if (obj.event === "guess") {
      var index = layer.open({
        type: 2,
        title: "竞猜管理(比赛ID: " + obj.data.id + ")",
        content: "/admin/guess/index_match?id=" + obj.data.id,
        maxmin: true,
      });
      layer.full(index);
    }
  });

  table.on("tool(LAY-football-recmatch-manage)", function (obj) {
    if (obj.event === "delrecmatch") {
      layer.confirm("确认取消推荐该比赛？", function (index) {
        admin.req({
          url: "/admin/basketball/delrecmatch_post",
          data: { id: obj.data.id },
          success(res) {
            if (res.code == 0) {
              table.reload("LAY-football-recmatch-manage");
              layer.msg(res.msg, { icon: 1 });
            } else {
              layer.msg(res.msg, { icon: 5 });
            }
          },
        });
      });
    } else if (obj.event === "anchor") {
      var index = layer.open({
        type: 2,
        title: "关联主播",
        content: "/admin/basketball/matchanchor?id=" + obj.data.match_id,
        maxmin: true,
      });
      layer.full(index);
    }
  });

  table.on("tool(LAY-football-matchanchor-manage)", function (obj) {
    if (obj.event === "del") {
      layer.confirm("确认取消关联该主播？", function (index) {
        admin.req({
          url: "/admin/basketball/delmatchanchor_post",
          data: { anchorid: obj.data.id, matchid: $("#matchid").val() },
          success(res) {
            if (res.code == 0) {
              table.reload("LAY-football-matchanchor-manage");
              layer.msg(res.msg, { icon: 1 });
            } else {
              layer.msg(res.msg, { icon: 5 });
            }
          },
        });
      });
    }
  });

  exports("basketball", {});
});

function formatSeconds(value) {
  var secondTime = parseInt(value); // 秒
  var minuteTime = 0; // 分
  var hourTime = 0; // 小时
  if (secondTime > 60) {
    //如果秒数大于60，将秒数转换成整数
    //获取分钟，除以60取整数，得到整数分钟
    minuteTime = parseInt(secondTime / 60);
    //获取秒数，秒数取佘，得到整数秒数
    secondTime = parseInt(secondTime % 60);
    //如果分钟大于60，将分钟转换成小时
    if (minuteTime > 60) {
      //获取小时，获取分钟除以60，得到整数小时
      hourTime = parseInt(minuteTime / 60);
      //获取小时后取佘的分，获取分钟除以60取佘的分
      minuteTime = parseInt(minuteTime % 60);
    }
  }
  var result =
    "" + parseInt(secondTime) >= 10
      ? parseInt(secondTime)
      : "0" + parseInt(secondTime);

  if (minuteTime > 0) {
    result =
      "" +
      (parseInt(minuteTime) >= 10
        ? parseInt(minuteTime)
        : "0" + parseInt(minuteTime)) +
      ":" +
      result;
  } else {
    result = "" + "00:" + result;
  }

  if (hourTime > 0) {
    result =
      "" +
      (parseInt(hourTime) >= 10
        ? parseInt(hourTime)
        : "0" + parseInt(hourTime)) +
      ":" +
      result;
  } else {
    result = "" + "00:" + result;
  }

  return result;
}

function formatTimeStamp(time_stamp) {
  const time = new Date(time_stamp * 1000);
  const Y = time.getFullYear();
  const M = (time.getMonth() + 1).toString().padStart(2, "0");
  const D = time.getDate().toString().padStart(2, "0");
  const h = time.getHours().toString().padStart(2, "0");
  const m = time.getMinutes().toString().padStart(2, "0");
  const s = time.getSeconds().toString().padStart(2, "0");
  return `${Y}-${M}-${D} ${h}:${m}:${s}`;
}
